package cn.tedu._03vehicle.controller;

import cn.tedu._03vehicle.base.response.JsonResult;
import cn.tedu._03vehicle.mapper.NoticeMapper;
import cn.tedu._03vehicle.pojo.vo.NoticeListVO;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.Serializable;
import java.util.List;
import java.util.concurrent.TimeUnit;

@Slf4j
@RestController
@Api(tags="03.公告管理模块")
@RequestMapping("/v1/notice/")
public class NoticeController {
    @Autowired
    NoticeMapper noticeMapper;
    @Autowired
    private RedisTemplate<String, Serializable> redisTemplate;
    @GetMapping("list")
    @ApiOperation(value="首页公告展示")
    @ApiOperationSupport(order=10)
    public JsonResult listNotice(){
        ValueOperations<String, Serializable> ops= redisTemplate.opsForValue();
        Serializable noticeIndex = ops.get("notice_index");
        if(noticeIndex !=null){
            log.debug("首页数据来自Redis数据库...");
            return JsonResult.ok(noticeIndex);
        }
        log.debug("首页数据来自Mysql数据库...");
        List<NoticeListVO> noticeListVOs = noticeMapper.listNotice();
        ops.set("notice_index",(Serializable)noticeListVOs,30, TimeUnit.SECONDS);
        return JsonResult.ok(noticeListVOs);
    }
}
